source ReplicaSim.tcl

define ReplicaSim method load { { nodelistfile '' }} {
    my variable myname
    my variable Nodes
    my variable Values
    my variable Dimensions
    my variable Path
    my variable confs
    # search for file myname.nodes if directory is given
    if {[string length $nodelistfile ]==0 } { set nodelistfile [pwd]}
    #defaul name is myname.nodes.
    if [file isdirectory $nodelistfile] { set nodelistfile [file join $nodelistfile $myname.nodes] }
    if ![file exists $nodelistfile] { puts "$nodelistfile does nod exist! Please specify a node list file to load.";error "nodelistfile not exist."}
    set myname [lindex [split [file rootname $nodelistfile] /] end]
    puts $myname
    set f [ open $nodelistfile r]
    set data [read $f]
    close $f
    #close opened charmm if exist?
    my loadNodelist $data
    #start charmm for analysis
    foreach {id info} $Nodes {
        if [info object isa object $id] { $id destroy }
        # read preparation code 
        if [file exists $Path($id)/$myname.run] { 
          set f [open $Path($id)/$myname.run]
          set prep_code [read $f]
          close $f
          set idx1 [regexp -inline -indices -- {UNBUFIO\s*\n} $prep_code]
          set startidx [lindex [lindex $idx1 0] 1]
          set idx2 [regexp  -inline -indices -- {\n\s+label begin} $prep_code]
          set endidx [lindex [lindex $idx2 0] 0]
          if {[string length $startidx ]==0} {set startidx 0}
          if {[string length $endidx ]==0} {set endidx [string length $prep_code]}
          set prep_code [string range $prep_code $startidx $endidx ]
          set prep_code [string map { "label TCLBLOCKFINISHED\n" ""} $prep_code]
          } else {
          puts "No run input file found! You need to setup the analysis environment yourself to match with replcia simulations."
          set prep_code "" 
        }
        # start analyzing nodes
    	Charmm create $id
    	if ![info exists confs] {set confs "bin imm1pb" }
	$id setup [ subst {
	    $confs
	    dir $Path($id)
	    cmd $myname.ana
	    output $myname.ana.out }]
        $id start 
        $id finished start 
        # prepare replica analyzing environment
        $id sendCmd $prep_code
    }
#    foreach {id info} $Nodes {
#     $id sendCmd $prep_code
#    }
#    # wait until preparation is done
    foreach {id info} $Nodes {
      $id finished sendCmd
    }
}

define ReplicaSim method getEnergyfromOutput {} {
  my variable Path 
  my variable myname
  my variable Nodes
  foreach node [array names Path] {
   puts "$Path($node)"
   set fin [open $Path($node)/$myname.run.out r]
#   set f [zlib push gunzip [open $Path($node)/$myname.run.out.gz r]]
   set data [read $fin]
   set idxs [regexp -all -indices -- {CHARMM>\s+label\s+begin([^\n]*)\n}]
   puts $idxs
   close $fin
  }
}

define ReplicaSim method analyze {} {

}


#helper
define ReplicaSim method loadNodelist { data} {
    my variable myname
    my variable Nodes
    my variable Values
    my variable Dimensions
    my variable Path
    # read title to determine vars
    if [regexp {^##([^\n]*)\n} $data matched title] {
       set vars {}
       foreach col $title {
         switch -glob $col {
          node {lappend vars node}
          path {lappend vars path}
          *(*) { regexp -- {(.*)\((.*)\)} $col matched varname depends
                 my addRelatedVarTo $depends $varname    
                 lappend vars $varname            
                }
          default { my hasDimension $col; lappend vars $col}
         }
       }
    } else {
       puts "No title found in $nodelistfile!"
    }
    # process data for Nodes
    foreach line [split $data \n] {
      if [regexp -- {^#} $line ] { continue}
      if ![parseToArray dataline $vars $line] { continue }
      foreach var [exclude $vars {node path}] {
        dict set Nodes $dataline(node) $var $dataline($var)
        addValue $var $dataline($var)
        if {[string length [my depend $var]] > 0} { 
         upvar [my varname $varname] Var
         set Var($dataline([my depend $var]))  $dataline($var)
        }
      }
      set Path($dataline(node)) $dataline(path)
    }
    foreach dim $Dimensions {
      set Values($dim) $var
    }
}


proc parseToArray { array title data} {
   upvar $array Array
   if {[llength $title] != [llength $data]} { return 0}
   for {set i 0} { $i <[llength $title]} { incr i} {
   set Array([lindex $title $i]) [lindex $data $i]
   }
   return 1
}
proc exclude { list toexclude} {
   set remain {}
   foreach item $list {
    if {[lsearch $toexclude $item] < 0} { lappend remain $item}
   }
   return $remain
}
proc addValue { list item} {
   upvar $list List
   if ![info exists List] {set List {}}
   if {[lsearch $List $item] < 0} { lappend List $item}
   set List [lsort $List]
}
